package webserver;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

public class Server extends ServerSocket implements Runnable, Logger {
	private HashMap<Integer, Service> services = new HashMap<Integer, Service>();
	public String documentRoot;
	private int serviceID = 1;

	public Server(String host, int port, String documentRoot) throws IOException {
		super(port, 1, InetAddress.getByName(host));
		this.documentRoot = documentRoot;
		Control.getInstance().log(this, this.toString());
	}

	public boolean isAlive() {
		return false;
	}

	public void removeService(int id) {

		Control.getInstance().log(this, "Service remove: " + services.toString());

		services.remove(id);
	}

	@Override
	public void run() {
		while (true) {
			try {

				Socket socket = this.accept();
				Service service = new Service(this, socket, serviceID);

				Thread thread = new Thread(service);
				thread.start();

				services.put(serviceID, service);
				
				Control.getInstance().log(this, "Services: " + services.toString());
				serviceID++;
			} catch (IOException e) {
				Control.getInstance().log(this, e.getMessage());
				break;
			}
		}
	}

	@Override
	public int getID() {
		return 0;
	}

}
